
from openpyxl import Workbook,load_workbook
import pickle
import random
import copy
save_data_dir=r"D:\Personal\Desktop\beifen"
wb = load_workbook('D:/Personal/Desktop/InputData.xlsx', read_only=True)
ws=wb['Pucks']
wc=wb['Gates']

class plane():
    def __init__(self,Fa,Fd,P,A,D,no,name,Dd_d,Dd_hb,Cf_d,Cf_hb):
        self.Fa = Fa  #到达类型
        self.Fd= Fd #出发类型
        self.P = P
        self.A = A  #到达时间
        self.D = D  #出发时间
        self.No=no
        self.name=name
        self.Dd_d=Dd_d
        self.Dd_hb=Dd_hb
        self.Cf_d=Cf_d
        self.Cf_hb=Cf_hb
        self.Cf_list=[]
        self.Dd_list=[]
class port():
    def __init__(self,Ha,Hd,G,no,name,port_l,port_aer):
        self.Ha = Ha
        self.Hd= Hd
        self.G = G
        self.No=no
        self.S_pl=[]
        self.name=name
        self.port_l=port_l
        self.port_aer=port_aer
class Tickets():
    def __init__(self,Dd_hb,Cf_hb,Dd_day,Cf_day,P_num,P_list):
        self.Dd_hb = Dd_hb #到达航班
        self.Cf_hb= Cf_hb #出发航班
        self.Dd_day = Dd_day
        self.Cf_day = Cf_day
        self.P_num=P_num
        self.P_list=P_list
        self.T_ftrans=None
        self.T_flow=None
        self.Cf_t = None  #航班的出发时间
        self.Dd_t = None   #到达时间
        self.Cf_l = None  #航班的出发类型
        self.Dd_l = None   #到达类型
        self.Dd_port = None #到达登机口终端厅
        self.Cf_port = None #出发登机口终端厅
        self.Cf_port_aer = None #出发登机口区域类型  
        self.Dd_port_aer = None #到达登机口区域类型
        self.T_tx = None #捷运时间  注意要*次数再 /5
        self.T_w = None #行走时间  注意要 /5
        self.n=None
    def flow(self,Cf_l,Dd_l,Dd_port,Cf_port):  #出发类型、航站楼（终端厅） 到达类型、到达航站楼
        if Cf_l==1: #出发国内D
            if Dd_l==1: #到达国内D
                if Cf_port=="T": #出发T
                    if Dd_port=="T": #到达T
                        return 3 ,0
                    else: #到达S
                        return 4 ,8/5
                else: #出发S
                     if Dd_port=="T": #到达T
                        return 4 ,8/5
                     else: #到达S
                        return 3 ,0
            else: #到达国际I
                if Cf_port=="T": #出发T
                    if Dd_port=="T": #到达T
                        return 7 ,0
                    else: #到达S
                        return 8 ,8/5
                else: #出发S
                     if Dd_port=="T": #到达T
                        return 8 ,8/5
                     else: #到达S
                        return 9 ,16/5

        else: #出发国际I
            if Dd_l==1: #到达国内D
                if Cf_port=="T": #出发T
                    if Dd_port=="T": #到达T
                        return 7 ,0
                    else: #到达S
                        return 8 ,8/5
                else: #出发S
                     if Dd_port=="T": #到达T
                        return 8 ,8/5
                     else: #到达S
                        return 7 ,0
            else: #到达国际I
                if Cf_port=="T": #出发T
                    if Dd_port=="T": #到达T
                        return 4 ,0
                    else: #到达S
                        return 6 ,8/5
                else: #出发S
                     if Dd_port=="T": #到达T
                        return 6 ,8/5
                     else: #到达S
                        return 4 ,0
    def out2(self):
        if self.Cf_t == None or self.Dd_t == None or self.Dd_port == None or self.Cf_port == None:
            self.T_flow=0
            self.T_ftrans=0
            return 0
        else:
            self.T_ftrans=self.Cf_t-self.Dd_t #航班连接时间 
            self.T_flow,self.T_tx=self.flow(self.Cf_l, self.Dd_l, self.Dd_port, self.Cf_port)               #最短流程时间
            if self.T_ftrans>=self.T_flow: # 换乘 成功
                return self.T_flow*self.P_num
            else: #换乘失败
                self.T_flow=72
                return 72

    def out3(self):
        i=0
        j=0
        if self.Dd_port=="T":
            if self.Dd_port_aer=="North":
                i=1
            elif self.Dd_port_aer=="Center":
                i=2
            elif self.Dd_port_aer=="South":
                i=3
        elif self.Dd_port=="S" :
            if self.Dd_port_aer=="North":
                i=4
            elif self.Dd_port_aer=="Center":
                i=5
            elif self.Dd_port_aer=="South":
                i=6
            elif self.Dd_port_aer=="East":
                i=7
        if self.Cf_port=="T":
            if self.Cf_port_aer=="North":
                j=1
            elif self.Cf_port_aer=="Center":
                j=2
            elif self.Cf_port_aer=="South":
                j=3
        elif self.Cf_port=="S" :
            if self.Cf_port_aer=="North":
                j=4
            elif self.Cf_port_aer=="Center":
                j=5
            elif self.Cf_port_aer=="South":
                j=6
            elif self.Cf_port_aer=="East":
                j=7
        a=[[10,15,20,25,20,25,25],
           [15,10,15,20,15,20,20],
           [20,15,10,25,20,25,25],
           [25,20,25,10,15,20,20],
           [20,15,20,15,10,15,15],
           [25,20,25,20,15,10,20],
           [25,20,25,20,15,20,10],]
            


                
        self.T_w=a[i-1][j-1]/5
        return self.T_w
    def n_out(self):
        self.out2()
        self.out3()
        if self.T_w==None or self.T_tx==None or self.T_flow==None or self.T_ftrans==None or self.T_ftrans==0:
            return 0
        else:        
            self.n=(self.T_w+self.T_tx+self.T_flow)/self.T_ftrans
            return self.n*self.P_num
def save_pickle_data(save_data_dir, save_data, dataname):
    # 保存数据集
    with open(save_data_dir + "/" + dataname + ".pickle", "wb") as handle:
        # pickle.dump(save_data, handle)
        pickle.dump(save_data, handle, protocol=pickle.HIGHEST_PROTOCOL)
    handle.close()
 
 
def read_pickle_data(save_data_dir, dataname):
    with open(save_data_dir + "/" + dataname + ".pickle", 'rb') as handle:
        read_data = pickle.load(handle)
    handle.close()
    return read_data


if __name__=='__main__':

    a=input("1输出宽窄+数量用于画图；2输出登机口飞机排列；3输出其他指标;4登机口停靠时间占比")

    value0,PORTS_resalt,ST_resalt,TICKETS_resolt=read_pickle_data(save_data_dir,"登机口结果2")
    print("读取历史最优值为：",value0)
    if a=="1":
        
        wb = Workbook()
        ws = wb.active
        for i in range(1,70):
            ws.cell(i,1).value=PORTS_resalt[i-1].No
            ws.cell(i,2).value=PORTS_resalt[i-1].name
            ws.cell(i,3).value=PORTS_resalt[i-1].G
            ws.cell(i,4).value=len(PORTS_resalt[i-1].S_pl)
        ws.cell(71,2).value="临时登机口"
        for i in range(3,len(ST_resalt)+3):
            j=i//7
            ws.cell(j+71,(i-3)%7+3).value=ST_resalt[i-3].name
        wb.save('D:/Personal/Desktop/第二问输出宽窄+数量.xlsx')

    elif a=="2":

        wb = Workbook()
        ws = wb.active
        for i in range(1,70):
            ws.cell(i,1).value=PORTS_resalt[i-1].No
            ws.cell(i,2).value=PORTS_resalt[i-1].name
            for j in range(3,len(PORTS_resalt[i-1].S_pl)+3):
                ws.cell(i,j).value=PORTS_resalt[i-1].S_pl[j-3].name

        ws.cell(71,2).value="临时登机口"
        for i in range(3,len(ST_resalt)+3):
            j=i//7
            ws.cell(j+71,(i-3)%7+3).value=ST_resalt[i-3].name
        wb.save('D:/Personal/Desktop/第二问飞机输出.xlsx')
    elif a=="3":

        wb = Workbook()
        ws = wb.active
        i=1
        j1=0
        j2=0
        j3=0
        j4=0
        j5=0
        j6=0
        j7=0
        j8=0
        j9=0
        j10=0
        num_p=0
        for pp in TICKETS_resolt:
            pp.out2()
            ws.cell(i,1).value=i
            ws.cell(i,2).value=pp.P_list
            ws.cell(i,3).value=pp.P_num
            ws.cell(i,4).value=pp.T_flow
            num_p+=pp.P_num
            if pp.T_flow>0 and pp.T_flow<=1:
                j1+=pp.P_num
            elif pp.T_flow>1 and pp.T_flow<=2:
                j2+=pp.P_num
            elif pp.T_flow>2 and pp.T_flow<=3:
                j3+=pp.P_num
            elif pp.T_flow>3 and pp.T_flow<=4:
                j4+=pp.P_num
            elif pp.T_flow>4 and pp.T_flow<=5:
                j5+=pp.P_num
            elif pp.T_flow>5 and pp.T_flow<=6:
                j6+=pp.P_num
            elif pp.T_flow>6 and pp.T_flow<=7:
                j7+=pp.P_num
            elif pp.T_flow>7 and pp.T_flow<=8:
                j8+=pp.P_num
            elif pp.T_flow>8 and pp.T_flow<=9:
                j9+=pp.P_num
            elif pp.T_flow>9 and pp.T_flow<=10:
                j10+=pp.P_num
            i+=1
        ws.cell(len(TICKETS_resolt)+1,1).value="总旅客数"
        ws.cell(len(TICKETS_resolt)+1,2).value="0-5"
        ws.cell(len(TICKETS_resolt)+1,3).value="5-10"
        ws.cell(len(TICKETS_resolt)+1,4).value="10-15"
        ws.cell(len(TICKETS_resolt)+1,5).value="15-20"
        ws.cell(len(TICKETS_resolt)+1,6).value="20-25"
        ws.cell(len(TICKETS_resolt)+1,7).value="25-30"
        ws.cell(len(TICKETS_resolt)+1,8).value="30-35"
        ws.cell(len(TICKETS_resolt)+1,9).value="35-40"
        ws.cell(len(TICKETS_resolt)+1,10).value="40-45"
        ws.cell(len(TICKETS_resolt)+1,11).value="45-100"

        ws.cell(len(TICKETS_resolt)+2,1).value=num_p
        ws.cell(len(TICKETS_resolt)+2,2).value=j1
        ws.cell(len(TICKETS_resolt)+2,3).value=j2
        ws.cell(len(TICKETS_resolt)+2,4).value=j3
        ws.cell(len(TICKETS_resolt)+2,5).value=j4
        ws.cell(len(TICKETS_resolt)+2,6).value=j5
        ws.cell(len(TICKETS_resolt)+2,7).value=j6
        ws.cell(len(TICKETS_resolt)+2,8).value=j7
        ws.cell(len(TICKETS_resolt)+2,9).value=j8
        ws.cell(len(TICKETS_resolt)+2,10).value=j9
        ws.cell(len(TICKETS_resolt)+2,11).value=j10
        wb.save('D:/Personal/Desktop/第二问旅客信息输出.xlsx')
        print("baocun")
    elif a=="4":
        wb = Workbook()
        ws = wb.active
        for i in range(1,70):
            ws.cell(i,1).value=PORTS_resalt[i-1].No
            ws.cell(i,2).value=PORTS_resalt[i-1].name
            sum=0
            for j in range(3,len(PORTS_resalt[i-1].S_pl)+3):
                plD=PORTS_resalt[i-1].S_pl[j-3].D
                plA=PORTS_resalt[i-1].S_pl[j-3].A
                if plD>288*2:
                    D=288*2
                else:
                    D=plD
                if plA<288:
                    A=288
                else:
                    A=plA
                sum+=D-A+9
            ws.cell(i,3).value=(sum-9)/288

        
        wb.save('D:/Personal/Desktop/第二问登机口航班停靠时间.xlsx')